import {defineStore} from 'pinia'
import {ref} from 'vue'
import {loginApi} from '@/api/login/index'
import {ElMessage} from 'element-plus'
import {useRouter} from 'vue-router'


export const userInfoStore = defineStore('userInfo', () => {
    let userInfo = ref({})
    const router = useRouter()

    //登录
    async function goLoginApi(data: object) {
        let res = await loginApi(data)
        if (res.msg = '操作成功') {
            userInfo.value = res.result
            ElMessage.success('登录成功')
            router.push('/')
        }
    }

    //退出登录
    function outLogin() {
        userInfo.value = {}
        router.push('/login')
    }

    return {
        goLoginApi,
        userInfo,
        outLogin
    }
}, {
    persist: true,
})